//
//  slidingViewCell.m
//  BeautifulWardrobe
//
//  Created by mac on 16/7/29.
//  Copyright © 2016年 rongshao. All rights reserved.
//

#import "slidingViewCell.h"

@interface slidingViewCell ()<UIScrollViewDelegate>

@end
@implementation slidingViewCell
//轮播滑动视图
- (UIScrollView *)shufflingScrollView
{
    if (!_shufflingScrollView)
    {
        self.shufflingScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 5)];
        _shufflingScrollView.contentSize = CGSizeMake(WIDTH*4, HEIGHT / 5);
        _shufflingScrollView.delegate = self;
        _shufflingScrollView.pagingEnabled = YES;
  
        
    }
    return _shufflingScrollView;
}
//轮播小圆点
- (UIPageControl *)pageControl
{
    if (!_pageControl)
    {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50 ,_shufflingScrollView.frame.size.height - 20 , 100, 10)];
        _pageControl.numberOfPages = 4;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    }
    return _pageControl;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [netRequestMethod sendRequestMethod:@"GET" withRequestURL:@"http://api.yuike.com/gmall/api/1.0/home/banner_list.php?mid=457465" withHttpBodyDic:nil withRequestBlock:^(NSData *data) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            _shufflingScrollViewArray = dic[@"data"][@"items"];
            [self ScrollImageView];
            [self addSubview:self.shufflingScrollView];
            //添加小圆点
            [self addSubview:self.pageControl];
        }];
         self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        
    }
    return self;
}
#pragma mark - 轮播视图
- (void)ScrollImageView
{
    for (int i = 0; i < 4; i++)
    {
        UIImageView *shufflingImage = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT / 5)];
        [shufflingImage sd_setImageWithURL:[NSURL URLWithString:_shufflingScrollViewArray[i][@"pic_url"]]];
        [self.shufflingScrollView addSubview:shufflingImage];
        
    }
}
//小圆点
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //通过滑动视图结束滑动后的最终偏移量除以屏幕的宽度得出偏移了几个屏幕宽度
    int currentPageNumber = scrollView.contentOffset.x / WIDTH;
    _pageControl.currentPage = currentPageNumber;
}
- (void)onTimer
{
    [UIView animateWithDuration:0.5 animations:^{
        static int speed = 1;
        _pageControl.currentPage += speed;
        _shufflingScrollView.contentOffset = CGPointMake(_pageControl.currentPage * WIDTH, 0);
        if (_pageControl.currentPage == 3 || _pageControl.currentPage == 0)
        {
            speed = -speed;
        }

    }];
}
//当拖拽时，销毁定时器，避免图片切换过快
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (_timer != nil)
    {
        [_timer invalidate];
        _timer = nil;
    }
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (_timer == nil)
    {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }
}

@end
